package com.steven.ourcontest.information.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.annotations.SerializedName
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.information.api.InfoApi
import com.steven.ourcontest.information.bean.IdentifyStatusResponse
import com.steven.ourcontest.information.bean.SchoolList
import com.steven.ourcontest.information.bean.UserNameResponse
import com.steven.ourcontest.information.bean.UserDetail
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.utils.Identity
import com.steven.ourcontest.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class IdentifyViewModel : BaseViewModel() {
    private val _schoolList = MutableLiveData<SchoolList?>()
    val schoolList: LiveData<SchoolList?>
        get() = _schoolList

    private val _addUserDetailResponse = MutableLiveData<EmptyResponse?>()
    val addUserDetailResponse: LiveData<EmptyResponse?>
        get() = _addUserDetailResponse

    private val _identifyStatus = MutableLiveData<IdentifyStatusResponse?>()
    val identifyStatus: LiveData<IdentifyStatusResponse?>
        get() = _identifyStatus

    private val _userName = MutableLiveData<UserNameResponse?>()
    val userName: LiveData<UserNameResponse?>
        get() = _userName

    fun querySchoolList(schoolName: String) {
        disposed += InfoApi.instance.querySchoolList(schoolName)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _schoolList.value = it.result
            }, {
                it.printStackTrace()
            })
    }

    fun addUserDetail(
        uname: String,
        school: String,
        college: String,
        judge: String="",
        startYear: Long=0,
        schoolType: String="",
        userType: String
    ) {
        disposed += InfoApi.instance.addUserDetail(
            LoginController.userId, uname, school,
            college,
            judge,
            startYear,
            schoolType,
            userType
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _addUserDetailResponse.value = it.result
            }, {
                it.printStackTrace()
            })
    }

    fun queryIdentifyStatus(uid: Long) {
        if (uid <= 0)
            return
        disposed += InfoApi.instance.queryIdentifyStatus(uid)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _identifyStatus.value = it.result
            }, {
                it.printStackTrace()
            })
    }

    fun queryUserDetail() {
        disposed += InfoApi.instance.queryUserDetail()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _userName.value = it.result
            }, {
                it.printStackTrace()
            })
    }
}